

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app33/page/home.dart';
import 'package:flutter_app33/req.dart';
import "dart:ui";

import 'model.dart';


void main() => runApp(MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  double _height = 300;
  int _selectedIndex = 0;
  Future<List<Album>> futureAlbum;
  final TextEditingController _user = TextEditingController();
  final TextEditingController _password = TextEditingController();
  final TextEditingController _from = TextEditingController();
  final TextEditingController _send = TextEditingController();
  final TextEditingController _value = TextEditingController();
  Future<Login> _loginAlbum;


  @override
  void initState() {
    super.initState();
    // _loginAlbum = loginAlbum("ss","ss");
    futureAlbum = fetchAlbum();


    print('我是日志');
    // print( futureAlbum.;

  }



  //显示对话框
  Future<void> _showAlert() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('提醒'),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[

                Text("账号或者密码输入错误请重新输入")

              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: Text('确定'),
              onPressed: () {
                Navigator.of(context).pop();
              },

            ),
          ],
        );
      },
    );
  }
  //显示对话框
  Future<void> _showMyDialog() async {
    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('树洞'),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                TextField(
                  controller: _send,
                  decoration: InputDecoration(
                      icon: Icon(Icons.person),
                      labelText: "发件人"

                  ),
                ),

                TextField(
                  controller: _from,
                  decoration: InputDecoration(
                      icon: Icon(Icons.person),
                      labelText: "接收人"

                  ),
                ),

                TextField(
                  controller: _value,
                  maxLines: 5,
                  decoration: InputDecoration(
                      icon: Icon(Icons.person),
                      labelText: "内容"

                  ),
                ),

              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: Text('确定'),
              onPressed: () {

                sendAlbum(_from.text, _value.text,_send.text);

                Navigator.of(context).pop();
                _from.clear();
                _value.clear();
                _send.clear();


                Future.delayed(Duration(seconds: 1), () {
                  setState(() {
                    futureAlbum = fetchAlbum();
                  });
                });


              },

            ),
            TextButton(
              child: Text('取消'),
              onPressed: () {

                Navigator.of(context).pop();

              },

            ),
          ],
        );
      },
    );
  }



  Future<Null> _handlerRefresh() async {
    setState(() {
      futureAlbum = fetchAlbum();
    });
    return null;
  }

  @override
  Widget build(BuildContext context) {

    //首页
    Widget home = RefreshIndicator(

      child: homet(futureAlbum),
        onRefresh: (){
          _handlerRefresh();
          return Future.value(true);
        },
    );



    Widget person = Container(
      margin: const EdgeInsets.only(top: 25),
        child:Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [


            Container(
              margin: EdgeInsets.only(left: 80),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Container(
                    height: 100,
                    child: Row(

                      crossAxisAlignment:CrossAxisAlignment.start,
                      children: [
                        Container(
                          width: 90.0,
                          height: 90.0,
                          decoration: BoxDecoration(
                            shape: BoxShape.circle,
                            image: DecorationImage(
                                fit: BoxFit.cover,
                                image: NetworkImage( 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202008%2F13%2F20200813003647_rCzJw.thumb.400_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1611736084&t=9c95ffc4fd138dceed7e14f95810c33c')
                            ),
                          ),
                        ),

                        Container(
                          margin: EdgeInsets.only(left: 20),
                        ),

                        Column(


                          crossAxisAlignment:CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,


                          children: [
                            Text(
                              "Woods",
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontWeight: FontWeight.w700,
                                fontSize: 20,

                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 13),
                            ),
                            Text(
                              "A young boy ",
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                fontSize: 15,
                              ),
                            ),
                          ],
                        ),

                        Container(

                        ),


                      ],
                    ),
                  ),


                  Container(
                    margin: EdgeInsets.only(left: 15,top: 10),
                    child: Row(

                      children: [

                        Text(
                            "500",

                            style: TextStyle(
                              fontWeight: FontWeight.w700,
                              fontSize: 20,

                            ),
                        ),

                        Text(
                          "已发信件",

                          style: TextStyle(

                            fontSize: 10,

                          ),
                        ),

                        Container(
                          margin: EdgeInsets.only(left: 75),
                        ),
                        Text("300",
                          style: TextStyle(
                            fontWeight: FontWeight.w700,
                            fontSize: 20,
                          ),
                        ),
                        Text(
                          "已阅读",

                          style: TextStyle(

                            fontSize: 10,

                          ),
                        ),

                      ],
                    ),
                  ),
                ],
              ),
            ),

            Container(

              margin: EdgeInsets.only(top: 60),

              child: CupertinoButton(
                child:Text("退出"),
                color: Colors.redAccent,                minSize: 10,
                onPressed: (){
                  setState(() {
                    _loginAlbum = null;
                  });

                },
              ),
            )


          ],

        ),

    );



    Widget login = Center(

      child: (_loginAlbum == null) ? Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [

          TextField(
            controller: _user,
            decoration: InputDecoration(
                icon: Icon(Icons.person),
                labelText: "账号"

            ),

          ),
          TextField(
            controller: _password,
            obscureText: true,
            decoration: InputDecoration(
                icon: Icon(Icons.handyman),
                labelText: "密码"
            ),

          ),
          Container(
              margin: const EdgeInsets.only( top: 10),

              child:  Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  ElevatedButton(
                    child: Text('登陆'),
                    onPressed: () {
                      setState(() {
                        _loginAlbum = loginAlbum(_user.text,_password.text);
                      });
                    },
                  ),
                ],
              )
          ),



        ],
      ):FutureBuilder<Login>(
          future: _loginAlbum,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              if (snapshot.data.flag == "1") {
                return person;
              } else {
              
              
                return Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [

                    TextField(
                      controller: _user,
                      decoration: InputDecoration(
                          icon: Icon(Icons.person),
                          labelText: "账号"

                      ),

                    ),
                    TextField(
                      controller: _password,
                      obscureText: true,
                      decoration: InputDecoration(
                          icon: Icon(Icons.handyman),
                          labelText: "密码"
                      ),

                    ),
                    Container(
                        margin: const EdgeInsets.only( top: 10),

                        child:  Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text("登录失败，请重新登录"),
                            ElevatedButton(
                              child: Text('登陆'),
                              onPressed: () {
                                setState(() {
                                  _loginAlbum = loginAlbum(_user.text,_password.text);
                                });
                              },
                            ),
                          ],
                        )
                    ),



                  ],
                );

              }
            } else if (snapshot.hasError) {
              return Text("${snapshot.error}");
            }

            return CircularProgressIndicator();
          }
      )
      ,

    );


    //账户
    Widget access = Container(

        child: login

    );


    const TextStyle optionStyle =
    TextStyle(fontSize: 30, fontWeight: FontWeight.bold);

    Widget explore = Container(
      margin: EdgeInsets.only(top:30),
      child: ListView(
        children: [
          Image.network("http://attach.bbs.miui.com/forum/201311/24/215347n3kjpxpj3220jj9r.jpg"),

          Container(
            margin: EdgeInsets.only(top: 10),
          ),

          Image.network("https://img.leitu123.com/article/UploadPic/2012-8/2012891165344737.jpg"),

          Container(
            margin: EdgeInsets.only(top: 10),
          ),
          Image.network("http://pic1.win4000.com/wallpaper/4/54865b13cea47.jpg"),
        ],
      ),
    );

    List<Widget> _widgetOptions = <Widget>[
      home,
      explore,
      access,
    ];

    void _onItemTapped(int index) {
      setState(() {
        _selectedIndex = index;
      });
    }
    _height = MediaQuery.of(context).size.height;
    return Scaffold(
      body: Container(
        child: _widgetOptions.elementAt(_selectedIndex),

      ),

      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.mail),
            label: '信件',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.explore),
            label: '探索',
          ),
          BottomNavigationBarItem(
            icon: Icon(CupertinoIcons.person_circle),
            label: '个人',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () {

          print("点击发信");
           _showMyDialog();

          // Add your onPressed code here!
        },
        label: Text('发信件'),
        icon: Icon(Icons.send),
        backgroundColor: Colors.blueAccent,
      ),

    );

  }
}
